unit fmunicipiocadastro;

{$mode objfpc}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, DBCtrls, ExtCtrls, Buttons;

type

  { TFormMunicipioCadastro }

  TFormMunicipioCadastro = class(TForm)
    ButtonInserir: TBitBtn;
    ButtonExcluir: TBitBtn;
    ButtonEditar: TBitBtn;
    ButtonCancelar: TBitBtn;
    ButtonSair: TBitBtn;
    ButtonSalvar: TBitBtn;
    ButtonLocalizar: TBitBtn;
    DBIdMunicipio: TDBEdit;
    DBNomeMunicipio: TDBEdit;
    DBUf: TDBEdit;
    DBNavigator1: TDBNavigator;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Panel1: TPanel;
    procedure ButtonCancelarClick(Sender: TObject);
    procedure ButtonEditarClick(Sender: TObject);
    procedure ButtonExcluirClick(Sender: TObject);
    procedure ButtonInserirClick(Sender: TObject);
    procedure ButtonLocalizarClick(Sender: TObject);
    procedure ButtonSairClick(Sender: TObject);
    procedure ButtonSalvarClick(Sender: TObject);
    procedure ControlaBotoes(Edicao: boolean);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormMunicipioCadastro: TFormMunicipioCadastro;

implementation

uses
  dmcadastro;

{ TFormMunicipioCadastro }

procedure TFormMunicipioCadastro.ButtonLocalizarClick(Sender: TObject);
begin
  DataCadastro.MunicipioBusca();
  ControlaBotoes(False);
end;

procedure TFormMunicipioCadastro.ButtonSairClick(Sender: TObject);
begin
  Close();
end;

procedure TFormMunicipioCadastro.ButtonSalvarClick(Sender: TObject);
begin
  DataCadastro.MunicipioSalvar();
  ControlaBotoes(False);
end;

procedure TFormMunicipioCadastro.ControlaBotoes(Edicao: boolean);
begin
  if Edicao then
  begin
    ButtonSair.Enabled := False;
    ButtonEditar.Enabled := False;
    ButtonExcluir.Enabled := False;
    ButtonLocalizar.Enabled := False;
    ButtonInserir.Enabled := False;
    ButtonSalvar.Enabled := True;
    ButtonCancelar.Enabled := True;
  end
  else
  begin
    ButtonSair.Enabled := True;
    ButtonLocalizar.Enabled := True;
    ButtonInserir.Enabled := True;
    ButtonSalvar.Enabled := False;
    ButtonCancelar.Enabled := False;
    if (DataCadastro.ZQueryMunicipio.RecordCount > 0) then
    begin
      ButtonEditar.Enabled := True;
      ButtonExcluir.Enabled := True;
    end;
  end;
end;

procedure TFormMunicipioCadastro.ButtonInserirClick(Sender: TObject);
begin
  DataCadastro.MunicipioInserir();
  ControlaBotoes(True);
end;

procedure TFormMunicipioCadastro.ButtonExcluirClick(Sender: TObject);
begin
  if (MessageDlg('Confirmação', 'Deseja excluir o município cadastrado?',
    mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
  begin
    DataCadastro.MunicipioExcluir();
    ControlaBotoes(False);
  end;
end;

procedure TFormMunicipioCadastro.ButtonEditarClick(Sender: TObject);
begin
  DataCadastro.MunicipioEditar();
  ControlaBotoes(True);
end;

procedure TFormMunicipioCadastro.ButtonCancelarClick(Sender: TObject);
begin
  DataCadastro.MunicipioCancelar();
  ControlaBotoes(False);
end;

initialization
  {$I fmunicipiocadastro.lrs}

end.
